package ast;

import java.util.LinkedList;

import out.Out;

import mem.interfaces.IMem;
import environnement.interfaces.IEnv;
import exceptions.LogoException;
import ast.interfaces.IAST;
import ast.interfaces.IASTfun;
import ast.interfaces.IASTfunrec;
import ast.interfaces.IASTif;
import ast.interfaces.IASTmove;
import ast.interfaces.IASTprint;
import ast.interfaces.IASTproc;
import ast.interfaces.IASTproccall;
import ast.interfaces.IASTprocrec;
import ast.interfaces.IASTprog;
import ast.interfaces.IASTset;
import ast.interfaces.IASTturn;
import ast.interfaces.IASTvar;
import ast.interfaces.IASTwhile;

public class ASTprog implements IASTprog{

	private LinkedList<IAST> e;

	public ASTprog(LinkedList<IAST> e) {
		super();
		this.e = e;
	}

	@Override
	public Object eval(Out o,IEnv env, IMem mem) throws LogoException{

		if(getE() != null ){
			for(IAST cmd : e){
				if(cmd instanceof ASTmove){
					IASTmove a = ((ASTmove) cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTturn){
					IASTturn a = ((ASTturn) cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTvar){
					IASTvar a = ((ASTvar)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTset){
					IASTset a = ((ASTset)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTif){
					IASTif a = ((ASTif)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTwhile){
					IASTwhile a = ((ASTwhile)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTfun){
					IASTfun a = ((ASTfun)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTfunrec){
					IASTfunrec a = ((ASTfunrec)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTproc){
					IASTproc a = ((ASTproc)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTprocrec){
					IASTprocrec a = ((ASTprocrec)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTprint){
					IASTprint a = ((ASTprint)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTprint){
					IASTprint a = ((ASTprint)cmd);
					a.eval(o,env, mem);
				}
				else if(cmd instanceof ASTproccall){
					IASTproccall a = ((ASTproccall)cmd);
					a.eval(o,env, mem);
				}
			}
		}
		return null;
	}

	@Override
	public String toTreeString(String ident,IEnv env,IMem mem) {
		if(getE() == null ){
			return "[]";
		}
		else {
			String ret="";
			int nb_cmd = e.size();
			
			for(IAST cmds : e){
				//affichage des commandes
				ret += ident + cmds.toTreeString(ident + "",env,mem);

				//gestion du ";"
				if(nb_cmd >1){
					ret +=";";
				}
				nb_cmd--;
			}
			return "[" + ret + "]";
		}
	}

	public LinkedList<IAST> getE() {
		return e;
	}

	public void setE(LinkedList<IAST> e) {
		this.e = e;
	}



}
